package com.example.dg.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upfile")
public class UpfileController {
    //要上传的文件
    @Value("${upfile.photoPath}")
    String path;
    @PostMapping("/photo")
    public String upphoto(@RequestParam MultipartFile file) throws IOException {
        //获得原始文件名 banner.jpg
        String originName = file.getOriginalFilename();
        //新文件名 6465696b-25d1-48ec-ac14-542378e3a286.jpg
        String filename = UUID.randomUUID().toString() + originName.substring(originName.lastIndexOf("."));
        //将文件保存到指定位置
        file.transferTo(new File(path+filename));
        //向客户端浏览器返回文件名

        return filename;
    }
}
